package com.pl.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.SysUser;
import com.pl.entity.WmStorageArea;
import com.pl.dao.WmStorageAreaDao;
import com.pl.lister.WmStorageAreaReadLister;
import com.pl.service.WmStorageAreaService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 库位表(WmStorageArea)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 15:00:09
 */
@Service("wmStorageAreaService")
public class WmStorageAreaServiceImpl implements WmStorageAreaService {
    @Autowired
    private WmStorageAreaDao wmStorageAreaDao;

    /**
     * 通过ID查询单条数据
     *
     * @param areaId 主键
     * @return 实例对象
     */
    @Override
    public WmStorageArea queryById(Long areaId) {
        return this.wmStorageAreaDao.queryById(areaId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<WmStorageArea> queryByPage(Map<String,Object> map) {
        return this.wmStorageAreaDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param wmStorageArea 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(WmStorageArea wmStorageArea, HttpSession session) {
        SysUser user=(SysUser) session.getAttribute("user");
        wmStorageArea.setCreateBy(user.getUserName());
        wmStorageArea.setCreateTime(new Date());
        wmStorageArea.setUpdateTime(new Date());
        int insert = this.wmStorageAreaDao.insert(wmStorageArea);
        if (insert>0){
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param wmStorageArea 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(WmStorageArea wmStorageArea) {
        int update = this.wmStorageAreaDao.update(wmStorageArea);
        if (update>0){
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param areaId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long areaId) {
        return this.wmStorageAreaDao.deleteById(areaId) > 0;
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int num =wmStorageAreaDao.deleteBatch(list);
        if (num>0){
            return true;
        }
        return false;
    }

    /**
     * 批量增加
     * @param list
     * @return
     */
    @Override
    public boolean insertBatch(List<WmStorageArea> list) {
        int num=wmStorageAreaDao.insertBatch(list);
        if (num>0){
            return  true;
        }
        return false;
    }

    @Override
    public String importArea(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),WmStorageArea.class,new WmStorageAreaReadLister(this)).sheet("库位信息").doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    /**
     * 文件导出
     * @param response
     */
    @Override
    public void exportArea(HttpServletResponse response) {
        List<WmStorageArea> wmStorageAreas=wmStorageAreaDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(),WmStorageArea.class).sheet("库位信息").doWrite(wmStorageAreas);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
